# Server program

from socket import *

#Event ID
ID_EXIT = 200

#ID's sent to server to differentiate difference in sent messages

ID_CONNECT  = 300
ID_EXIT     = 400
ID_CHAT     = 500


def getID(data):
	return int(data[:3])

def getMessage(data):
	return data[3:]


# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)
username = ""
userlist = []

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

# Receive messages
while 1:
	data,addr = UDPSock.recvfrom(buf)
	
	if(getID(data) == ID_CONNECT):
		username = getMessage(data)
		userlist.append(username)
		
	elif(getID(data) == ID_CHAT):
		print  username + ": " , getMessage(data)
		
	elif not data:
		print "Client has exited!"
		break
	else:
		print  username + ": " , getMessage(data)

# Close socket
UDPSock.close()






